package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.GoodsBrandDAO;
import com.ecoolex.ying.space.core.service.PickGoodsSearchService;
import com.ecoolex.ying.space.entity.GoodsBrand;
import com.ecoolex.ying.space.entity.GoodsBrandExample;
import com.ecoolex.ying.space.pojo.QueryGoodsBrandDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import static com.ecoolex.ying.space.entity.GoodsBrand.*;

@Service
public class GoodsBrandManager {
    @Autowired
    private GoodsBrandDAO dao;

    @Autowired
    private GoodsManager goodsManager;

    @Autowired
    private PickGoodsSearchService pickGoodsSearchService;

    private void verify(GoodsBrand record) {
        BizAssert.isTrue(Check.notNullOrTrimEmpty(record.getName()), ResultCode.VERIFY.build(10), "名称不能为空");
    }

    public GoodsBrand create(GoodsBrand record, String createUser) {
        verify(record);

        record.setDeleted(NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
        return record;
    }

    @Transactional
    public void update(GoodsBrand record, String updateUser) {
        verify(record);

        GoodsBrand old = getById(record.getBrandId());

        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        int row = dao.updateByPrimaryKeySelective(record,
                Column.excludes(Column.deleted,
                        Column.createUser,
                        Column.createTime));
        BizAssert.isTrue(row == 1, "修改品牌失败");

        pickGoodsSearchService.changeBrand(old, record);
    }

    public void delete(GoodsBrand record, String updateUser) {
        long countGoodsUsedNumber = goodsManager.countByBrandId(record.getBrandId());
        BizAssert.isTrue(countGoodsUsedNumber == 0, ResultCode.VERIFY.build(10), "品牌有{}个商品使用,无法删除", countGoodsUsedNumber);

        record.setDeleted(IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.updateByPrimaryKeySelective(record, Column.deleted, Column.updateUser, Column.updateTime);
    }

    public GoodsBrand getById(Integer brandId) {
        return dao.selectByPrimaryKeyWithLogicalDelete(brandId, NOT_DELETED);
    }

    public Pagination<GoodsBrand> query(QueryGoodsBrandDTO query, Sorting[] sorts, Page page) {
        GoodsBrandExample example = new GoodsBrandExample();
        GoodsBrandExample.Criteria criteria = example.createCriteria();

        if (Check.notNullOrTrimEmpty(query.getName())) {
            criteria.andNameLike('%' + query.getName()
                    + '%');
        }
        if (Check.notNull(query.getClassifyId())) {
            criteria.andClassifyIdEqualTo(query.getClassifyId());
        }
        criteria.andDeletedEqualTo(NOT_DELETED);

        example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());

        return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
    }

    public List<GoodsBrand> queryAll() {
        GoodsBrandExample example = new GoodsBrandExample();
        GoodsBrandExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(NOT_DELETED);

        return dao.selectByExample(example);
    }
}